package com.dp.appi.service.impl;

import com.dp.appi.dao.ProjectDao;
import com.dp.appi.exception.CommonException;
import com.dp.appi.exception.ProjectRuntimeException;
import com.dp.appi.service.ProjectService;
import com.dp.appi.util.SYS;
import com.dp.appi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/4/27.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    /**
     * 获取个人项目列表
     *
     * @param id 当前用户id
     * @param s  开始序列
     * @param n  获取条数
     * @return
     */
    @Override
    public List<Map<String, Object>> getSingleProject(String id, int s, int n,String project_name,Object org_id) {
        return projectDao.getSingleProject(id,s,n,project_name,org_id);
    }

    /**
     * 条件查询个人项目
     *
     * @param id    当前用户id
     * @param s     开始序列
     * @param n     获取条数
     * @param param 查询参数
     * @return
     */
    @Override
    public List<Map<String, Object>> getProjectByParam(String id, int s, int n, Map<String, String> param,Object org_id) {
        return projectDao.getProjectByParam(id,s,n,param,org_id);
    }

    /**
     * 创建项目
     * @param param
     */
    @Override
    public Map<String,Object> createProject(Map<String, String> param,String id) {


        //创建项目
        Long project_id  = projectDao.createPBaseinfo(param,id);
        //创建阶段
        Long stage_id = projectDao.createPStage(param,project_id);
       /* //创建土质信息
        if(!StringUtils.isEmpty(param.get("soil_type"))){
            projectDao.createPSoil(param,project_id);
        }
        //创建二次转运
        if(!StringUtils.isEmpty(param.get("transport_type"))) {
            projectDao.createPSecondTransport(param, project_id);
        }*/
        //创建项目进度
        projectDao.createPSchedule(param,project_id,stage_id);

        Map<String,Object> map = new HashMap<>();
        map.put("project_id",project_id);
        map.put("stage_id",stage_id);
        return map;
    }

    /**
     * 删除项目阶段
     *
     * @param stage_id 阶段id
     * @param id       当前用户id
     */
    @Override
    public void delProjectStage(Long stage_id, String id) {
        projectDao.delProjectStage(stage_id,id);
    }

    /**
     * 查询项目阶段信息
     *
     * @param stage_id 项目阶段信息
     * @return
     */
    @Override
    public Map<String, Object> projectInfo(Long stage_id) {
        Map<String, Object> resMap = projectDao.projectInfo(stage_id);
        return resMap;
    }

    /**
     * 修改项目阶段
     *
     * @param param
     * @param param
     */
    @Override
    public void upProject(Map<String, Object> param) {

        //判断项目是否存在
        Map<String,Object> map = projectDao.projectInfo(Long.parseLong(String.valueOf(param.get("stage_id"))));
        if(map==null || StringUtils.isEmpty(map.get("project_id"))){
            throw new ProjectRuntimeException(SYS.PRO001.name());
        }
        param.put("project_id",String.valueOf(map.get("project_id")));
        //保存项目
//        projectDao.upProject(param,id);
        Map condiMap = new HashMap();
        condiMap.put("project_id",String.valueOf(map.get("project_id")));
        param.remove("project_id");
        projectDao.upCommField(param,"p_baseinfo",condiMap);


        //保存项目阶段
//        projectDao.upProjectStage(param,id);
        Map stageMap = new HashMap();
        stageMap.put("project_id",String.valueOf(map.get("project_id")));
        stageMap.put("stage_id",String.valueOf(param.get("stage_id")));
        param.remove("stage_id");
        projectDao.upCommField(param,"p_stage",stageMap);


        //保存土壤
        if(param.containsKey("soil")&&StringUtil.isNotEmpty(param.get("soil"))) {
            projectDao.upProjectSoil(String.valueOf(param.get("soil")), String.valueOf(param.get("project_id")));
        }

        //保存二次转运
        if(param.containsKey("transport")&&StringUtil.isNotEmpty(param.get("transport"))) {
            projectDao.upProjectSecondTransport(String.valueOf(param.get("transport")), String.valueOf(param.get(param.get("project_id"))));
        }
    }
    @Override
    public void upProjectVersionid(long codeset_version_id , long  project_id) {
        projectDao.upProjectVersionid(codeset_version_id,project_id);
    }
    /**
     * 获取数据权限类别
     *
     * @param id
     * @return
     */
    @Override
    public int rabcClassify(String id, long stage_id) {
        Map<String,Object> rabcM = projectDao.rabcClassify(id,stage_id);
        if(rabcM == null){
            return 0;// 无权限
        }
        return  Integer.parseInt(String.valueOf(rabcM.get("classify")));
    }

    /**
     * 获取最近一次的定位点踏勘数据
     *
     * @param id
     * @param stage_id
     * @param classify
     * @return
     */
    @Override
    public Map<String, Object> getLastExp(Object id, Long stage_id, int classify,Integer pt_type) {
        return projectDao.getLastExp(id, stage_id, classify,pt_type);
    }

    /**
     * 条件查询个人所有项目
     *
     * @param id
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllProjectByParam(String id, Map<String, String> param,Object org_id) {
        return projectDao.getAllProjectByParam(id,param,org_id);
    }

    /**
     * 查询阶段的基本信息
     *
     * @param stage_id 项目阶段信息
     * @return
     */
    @Override
    public Map<String, Object> projectStageInfo(Long stage_id) {
        return projectDao.projectStageInfo(stage_id);
    }


}
